import Ab_NativeClient_bA, { Ab_NativeClientIns_bA } from "../common/NativeClient";

/** 原生端行为模拟 */
class Ab_VirtualClient_bA extends Ab_NativeClientIns_bA {
	private static Ab_response_cache_bA: Map<string, [abi.Ab_NudityPromise_bA<any>, any]> = new Map();
	private static Ab_used_token_bA: string[] = [];

	public Ab_callStaticMethod_bA(api: string, data: any, ccdata?: any){
		return Ab_VirtualClient_bA.Ab_request_bA(api, data, ccdata);
	}

	private static Ab_request_bA(Ab_api_bA: string, Ab_data_bA: any, ccdata?: any){

		let token = abi.Ab_string_bA.Ab_randomToken_bA(5, 36, t=>!Ab_VirtualClient_bA.Ab_used_token_bA.includes(t)).toUpperCase();
		let np = new abi.Ab_NudityPromise_bA();
		Ab_VirtualClient_bA.Ab_response_cache_bA.set(token, [np, ccdata]);

		let Ab_request_bA = {
			Ab_cc_token_bA: token,
			Ab_data_bA
		};
		Ab_VirtualClient_bA.Ab_virtualFunc_bA(Ab_api_bA, JSON.stringify(Ab_request_bA));
		return {Ab_api_bA, Ab_request_bA, Ab_response_bA: np.Ab_promise_bA};
	}

	private static Ab_response_bA(api: string, json_string: string){
		let response = JSON.parse(json_string);
		let token = response.Ab_cc_token_bA;
		let [np, ccdata] = Ab_VirtualClient_bA.Ab_response_cache_bA.get(token);
		Ab_VirtualClient_bA.Ab_response_cache_bA.delete(token);
		response.Ab_cc_token_bA = ccdata ?? token;

		np.Ab_resolve_bA(response);
	}

	/** 原生端功能模拟 */
	private static Ab_virtualFunc_bA(api: string, json_string: string){
		if(!Reflect.has(Ab_VirtualClient_bA, api)) return void 0;

		let request = JSON.parse(json_string);
		let response: any = Reflect.apply(Reflect.get(Ab_VirtualClient_bA, api), Ab_VirtualClient_bA, [Object.assign({}, request)]);

		if(response) Ab_VirtualClient_bA.Ab_response_bA(api, JSON.stringify(response));
		else Ab_VirtualClient_bA.Ab_response_cache_bA.delete(request.Ab_cc_token_bA)
	}

	private static Ab_setRESData_bA(response: any, data: any){
		Reflect.set(response, 'Ab_data_bA', data);
	}

	/** 模拟广告行为 */
	private static Ab_AD_bA(response: any){
		Ab_VirtualClient_bA.Ab_setRESData_bA(response, {
			Ab_status_bA: 'ad-over',
		});
		return response;
	}

	/** 模拟本地化行为 */
	private static Ab_LocalLanguage_bA(response: any){
		Ab_VirtualClient_bA.Ab_setRESData_bA(response, {
			Ab_countryCode_bA: 'CN',
			Ab_languageCode_bA: 'zh',
		});
		return response;
	}

	/** 模拟链接行为 */
	private static Ab_Link_bA(response: any){
		window.open(response.Ab_data_bA.url);
		return undefined;
	}

	/** 模拟获取设备ID */
	private static Ab_device_id_bA(response: any){
		let virtual_device_ID = localStorage.getItem('VIRTUAL-DEVICE-ID');
		if(!virtual_device_ID){
			virtual_device_ID = [8, 4, 4, 4, 12].map(len=>abi.Ab_string_bA.Ab_random_bA(len, 16).toUpperCase()).join('-');
			virtual_device_ID = '<chrome-virtual>['+virtual_device_ID+']';
			localStorage.setItem('VIRTUAL-DEVICE-ID', virtual_device_ID);
		}
		Ab_VirtualClient_bA.Ab_setRESData_bA(response, {
			Ab_device_id_bA: virtual_device_ID,
		});
		return response;
	}

	/** 模拟奖励领取 */
	private static Ab_fruit_bA(){
		return undefined;
	}

	/** 模拟弹窗浮层 */
	private static Ab_float_bA(){
		return undefined;
	}

	private static Ab_set_clipboard_bA(response: any){
		abi.Ab_log_bA.Ab_tag_bA('设置剪贴板: green;', response.Ab_data_bA.Ab_text_bA);
		return undefined;
	}

	private static Ab_type_bA(response: any){
		Ab_VirtualClient_bA.Ab_setRESData_bA(response, {
			Ab_type_bA: 'Test',
		});
		return response;
	}
}

Ab_NativeClient_bA.Ab_initClient_bA(platform=>{
	if(platform=='virtual'){
		return new Ab_VirtualClient_bA();
	}
});
